package com.beiing.tianshuai.tianshuai.dongtai.presenter;

import com.beiing.tianshuai.tianshuai.base.BasePresenter;
import com.beiing.tianshuai.tianshuai.dongtai.CallBack;
import com.beiing.tianshuai.tianshuai.dongtai.model.DynamicDetailModel;
import com.beiing.tianshuai.tianshuai.dongtai.model.DynamicDetailModelImpl;
import com.beiing.tianshuai.tianshuai.dongtai.view.DynamicDetailViewImpl;
import com.beiing.tianshuai.tianshuai.entity.DynamicAddReplyBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicDeleteBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicDetailBean;
import com.beiing.tianshuai.tianshuai.entity.DynamicPraiseBean;
import com.beiing.tianshuai.tianshuai.entity.FollowBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.rx.MsgEvent;
import com.beiing.tianshuai.tianshuai.rx.RxBus;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.UnsupportedEncodingException;

import io.reactivex.disposables.Disposable;

/**
 * Created by LZQ on 2017/8/2.
 */

public class DynamicDetailPresenter extends BasePresenter implements DynamicDetailPresenterImpl {

    private static final String TAG = "DynamicDetailPresenter";
    private DynamicDetailViewImpl mView;
    private DynamicDetailModelImpl mModel;
    private DynamicDetailBean dynamicDetailBean;

    public DynamicDetailPresenter(DynamicDetailViewImpl mView) {
        this.mView = mView;
        this.mModel = new DynamicDetailModel();
    }

    /*
        获取动态详情
     */
    @Override
    public void getDynamicDetail(String uid, String id, int type, int page, int num, final boolean isLoadmore) {
        mModel.getDynamicDetail(uid, id, type, page, num, new CallBack<DynamicDetailBean>() {
            @Override
            public void result(DynamicDetailBean bean) {
                mView.hideProgress();
                if (bean.getCode() == 200){
                    try {
                        if (isLoadmore){
                            mView.loadMoreComment(bean.getData().getComment());
                        } else {
                            mView.showDynamicDetail(bean);
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } else {
                    mView.showDynamicNotExist();
                }
            }

            @Override
            public void error(Throwable e) {
                mView.hideProgress();
                mView.showError("数据获取出错，请稍后重试");
            }

        });
    }

    /*
        添加评论
     */
    @Override
    public void addComment(final String uid, final String to_uid, final String aid, String content, String register_id, final String create_time, final int position) {
        mModel.addComment(uid, to_uid, aid, content, register_id, create_time, new CallBack<DynamicCommentBean>() {
            @Override
            public void result(DynamicCommentBean bean) {
                if (position != -1) {
                    //更新动态列表的评论
                    RxBus.getInstanceBus().post(new MsgEvent<>("dynamic_detail_comment", bean, position));
                }
                mView.refreshComment(bean);
                LogUtils.i(position+"", "评论1");
                sendPush("评论", Integer.parseInt(to_uid), Integer.parseInt(bean.getData().getId()), "dynamic");
            }

            @Override
            public void error(Throwable e) {
                mView.showError("评论失败");
            }

        });
    }

    /*
        添加回复
     */
    @Override
    public void addReply(final String did, String cid, final String form_uid, String to_uid, String content, String registration_id) {
        mModel.addReply(did, cid, form_uid, to_uid, content, registration_id, new CallBack<DynamicAddReplyBean>() {

            @Override
            public void result(DynamicAddReplyBean bean) {
//                mView.refreshComment(bean);
                getDynamicDetail(form_uid, did,0,0,20,false);
            }

            @Override
            public void error(Throwable e) {
                mView.showToast("回复失败");
            }
        });
    }

    /*
        点赞动态
     */
    @Override
    public void praiseDynamic(final String userid, final String aid, final String to_uid, final int position) {
        mModel.praiseDynamic(userid, aid, to_uid, new CallBack<DynamicPraiseBean>() {

            @Override
            public void result(DynamicPraiseBean bean) {
                mView.showDynamicPraiseResult(bean);
                RxBus.getInstanceBus().post(new MsgEvent<>("dynamic_detail_praise", bean.getCode(), position));
                if (bean.getCode() == 200){
                    sendPush("点赞", Integer.parseInt(to_uid), Integer.parseInt(aid), "dynamic");
                }
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    /*
        删除评论
     */
    @Override
    public void deleteComment(final String uid, final String id, final String did, final int position, final int dynamic_position) {
        mModel.deleteComment(uid, id, did, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {
                mView.deleteComment(bean, position);
                if (dynamic_position != -1) {
                    RxBus.getInstanceBus().post(new MsgEvent<>("dynamic_detail_comment_delete", id, dynamic_position));
                }
            }

            @Override
            public void error(Throwable e) {
                mView.showToast("删除失败");
            }
        });
    }

    @Override
    public void isFollowed(String fromUid, String toUid) {
        mModel.isFollowed(fromUid, toUid, new CallBack<FollowBean>() {

            @Override
            public void result(FollowBean bean) {
                mView.showFollowed(bean);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void setFollowed(final String fromUid, final String toUid) {
        mModel.setFollowed(fromUid, toUid, new CallBack<FollowResultBean>() {

            @Override
            public void result(FollowResultBean bean) {
                isFollowed(fromUid, toUid);
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void deleteReply(String uid, String id, final int position, final int childPosition) {
        mModel.deleteReply(uid, id, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {
                mView.deleteReply(position, childPosition);
            }

            @Override
            public void error(Throwable e) {
                mView.showError("删除回复失败");
            }
        });
    }

    @Override
    public void praiseComment(String uid, String cid, String to_uid, String did, final int comment_position) {
        mModel.praiseComment(uid, cid, to_uid, did, new CallBack<DynamicPraiseBean>() {

            @Override
            public void result(DynamicPraiseBean bean) {
                mView.showCommentPraiseResult(bean, comment_position);
            }

            @Override
            public void error(Throwable e) {
                mView.showError("点赞失败");
            }
        });
    }


    /*
        发送极光推送
     */
    @Override
    public void sendPush(String status, int tag, int id, String type) {
        mModel.sendPush(status, tag, id, type, new CallBack<DynamicDeleteBean>() {

            @Override
            public void result(DynamicDeleteBean bean) {
                LogUtils.i(bean.getCode()+"", "sendPush");
            }

            @Override
            public void error(Throwable e) {

            }
        });
    }

    @Override
    public void addSubscription(Disposable disposable) {
        super.addSubscription(disposable);
    }
}
